#include "qmltableview.h"
#include <QDebug>
#include <QTimer>
QmlTableView::QmlTableView(QObject *parent) : QAbstractTableModel(parent)
{

    QTimer::singleShot(5000,this, SLOT(initData()));

//    initData();
}

int QmlTableView::rowCount(const QModelIndex &parent) const
{
//    qDebug() << "rowCount" << m_tableList.size();
    return m_tableList.size();
}

int QmlTableView::columnCount(const QModelIndex &parent) const
{
//    qDebug() << "columnCount" << m_headList.size();
    return m_headList.size();
}

QVariant QmlTableView::data(const QModelIndex &index, int role) const
{

    /////////////////////////////////////////////
    /// 这里输出的column全是0，没办法只能用role代替 ///
    /////////////////////////////////////////////

    if(!index.isValid())
    {
        return QVariant();
    }


    switch (role) {
    case Role::OneRole:
        return m_tableList.at(index.row())->index;
        break;
    case Role::TwoRole:
        return m_tableList.at(index.row())->data;
        break;
    case Role::ThreeRole:
        return m_tableList.at(index.row())->title;
        break;
    case Role::ThridRole:
        return m_tableList.at(index.row())->date;
        break;
    default:
        qDebug() << "data" << "empty";
        return QVariant();
        break;
    }

}

QVariant QmlTableView::headerData(int section, Qt::Orientation orientation, int role) const
{

    qDebug() << "headerData";
    if(orientation==Qt::Horizontal&&role==Qt::DisplayRole){
           return m_headList.at(section);
       }else{
           return QAbstractTableModel::headerData(section,orientation,role);
       }
}



QHash<int, QByteArray> QmlTableView::roleNames() const
{
    QHash<int, QByteArray> roles;
       roles[Role::OneRole] = "numberRole";
       roles[Role::TwoRole] = "DataRole";
       roles[Role::ThreeRole] = "HeadRole";
       roles[Role::ThridRole] = "DateRole";
       return roles;
}

void QmlTableView::refResh()
{
    beginResetModel();
    endResetModel();
}

void QmlTableView::initData()
{
    m_headList.append("编号");
    m_headList.append("内容");
    m_headList.append("标题");
    m_headList.append("日期");


    qDebug() << "init data";

//   for(int row=0; row<10; row++)
//   {
//     QStringList rowsList;
//    for(int i=0; i<m_headList.size(); i++)
//    {
//        rowsList.append(QString("(%1,%2)").arg(row).arg(i));
//    }
//    m_tableList.append(rowsList);
//   }

//   refResh(); //调用该函数会自动刷新（全局）表

    pRowData pData = new rowData;
    pData->index = m_tableList.size()+1;
    pData->data = "1111";
    pData->title = "hell0";
    pData->date = "20191212";
    sltAppendData(pData);

//     QTimer::singleShot(10,this, SLOT(initData()));


}

void QmlTableView::sltAppendData(pRowData rowData)
{

    if(rowData == nullptr)
    {
        clearData();
        return;
    }

    beginInsertRows(QModelIndex(),m_tableList.size(),m_tableList.size());
    rowData->index = m_tableList.size()+1;
    rowData->date = "20191212";
    rowData->title = rowData->title.split('/').last();
    m_tableList.append(rowData);
    endInsertRows();
}

void QmlTableView::clearData()
{

    foreach(pRowData data ,m_tableList){
        delete data;
        data = nullptr;
    }
    m_tableList.clear();

    refResh();
}



